<template>
  <view v-if="detailList.goods_name"  class="product-detail">
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,index) in detailList.pics" :key="item.goods_id" >
       <image :src="item.pics_big" @click="perview(index)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品描述 -->
    <view class="product-detail">
      <view class="product-price">￥{{detailList.goods_price}}</view>
      <view class="product-text">
        <view class="text">{{detailList.goods_name}}</view>
        <view class="product-icon">
          <uni-icons type="star" size="17" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="product-footer">快递：免运费</view>
    </view>
    <rich-text :nodes="detailList.goods_introduce"></rich-text>
  <view class="goodsNav">
      <uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
  </view>
  </view>
</template>

<script>
  import {mapState,mapMutations,mapGetters} from 'vuex'
  export default {
    computed:{
      ...mapState('user',['token']),
      ...mapGetters('cart',['infos'])
    },
    watch:{
      // infos(newvalue){
      //   const findResult=this.options.find(item=>item.text==='购物车')
      //   console.log(findResult);
      //   if(findResult){
      //     findResult.info=newvalue
      //   }
      // }
      infos:{
        handler(newvalue){
          const findResult=this.options.find(item=>item.text==='购物车')
            console.log(findResult);
            if(findResult){
              findResult.info=newvalue
            }
        },
        immediate:true
      }
    },
    data() {
      return {
        detailList:{},
         options: [{
        			icon: 'headphones',
        			text: '客服'
        		}, {
        			icon: 'shop',
        			text: '店铺',
        			infoBackgroundColor:'#007aff',
        			infoColor:"red"
        		}, {
        			icon: 'cart',
        			text: '购物车',
        			info: 0
        		}],
        	    buttonGroup: [{
        	      text: '加入购物车',
        	      backgroundColor: '#ff0000',
        	      color: '#fff'
        	    },
        	    {
        	      text: '立即购买',
        	      backgroundColor: '#ffa200',
        	      color: '#fff'
        	    }
        	    ]
      };
    },
    onLoad(options) {
      const goods_id=options.goods_id
      this.getdetailList(goods_id)
    },
    methods:{
      ...mapMutations('cart',['addCart']),
      ...mapMutations('user',['updateRedirectInfo']),
      async getdetailList(goods_id){
        const {data:res} = await uni.$http.get('/api/public/v1/goods/detail',{goods_id})
        if(res.meta.status!==200) return uni.$showMsg()
        res.message.goods_introduce=res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(/webp/g, 'jpg')
        
        this.detailList=res.message
      },
      perview(index){
        uni.previewImage({
          current:index,
          urls:this.detailList.pics.map(item=>item.pics_big)
        })
      },
      onClick(e){
        if(e.content.text==='购物车'){
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },
      buttonClick(e){
        // { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
        const goods={
          goods_id:this.detailList.goods_id,
          goods_name:this.detailList.goods_name,
          goods_price:this.detailList.goods_price,
          goods_count:1,
          goods_small_logo:this.detailList.goods_small_logo,
          goods_state:true
        }
        if(e.content.text==='加入购物车'){
          if(!this.token){
            this.showLogin()
            return
          }
        this.addCart(goods)
        }
      },
      async showLogin(){
         const [err,succ]=await uni.showModal({
           title:'提示',
           content:'您还未登录'
         }).catch(err=>err)
         if(succ&&succ.confirm) {
           uni.switchTab({
             url:'/pages/my/my',
             success:()=>{
               this.updateRedirectInfo({
                openType:'navigateTo',
                from:'/subpkg/goods_detail/goods_detail?goods_id=' + this.detailList.goods_id
               })
             }
           })
         }
       }
    }
  }
</script>

<style lang="scss">
swiper {
  height:  750rpx;
  image {
    width: 100%;
    height: 100%;
  }
}
.product-detail {
  padding: 15px 10px;
  .product-price {
    color: #C00000;
    font-size: 22px;
  }
  .product-text {
    display: flex;
    justify-content: space-between;
    padding: 10px 0;
    .text{
      font-size: 14px;
      border-right: 1px solid #efefef;
      padding-right: 5px;
    }
    .product-icon {
      width: 100px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      text {
        font-size: 12px;
        color: gray;
      }
    }
    
  }
  .product-footer {
    font-size: 12px;
    color: gray;
    padding: 5px 0;
  }
}
.product-detail {
  padding-bottom: 50px;
}
.goodsNav {
  position: fixed;
  width: 100%;
  bottom: 0;
}
</style>
